<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="98px"
    >
      <el-form-item label="退单号" prop="backCode">
        <el-input
          v-model="queryParams.backCode"
          placeholder="请输入退单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择退款／退货状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物流公司" prop="logisCompanyName">
        <el-input
          v-model="queryParams.logisCompanyName"
          placeholder="请输入物流公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流单号" prop="waybillCode">
        <el-input
          v-model="queryParams.waybillCode"
          placeholder="请输入物流单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8"> </el-row>

    <el-table
      v-loading="loading"
      :data="OmsBackOrderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="退单号" align="center" prop="backCode" />
      <el-table-column label="订单号" align="center" prop="orderCode" />
      <el-table-column label="用户id" align="center" prop="customerId" />
      <el-table-column
        label="类型"
        align="center"
        prop="type"
        :formatter="typeFormat"
      />
      <el-table-column label="退款／退货原因" align="center" prop="reason" />
      <el-table-column label="问题说明" align="center" prop="desc" />
      <el-table-column
        label="退款／退货金额"
        align="center"
        prop="backPrice"
        :formatter="backPriceFormat"
      />
      <el-table-column
        label="实际退款金额"
        align="center"
        prop="realBackPrice"
        :formatter="realBackPriceFormat"
      />
      <el-table-column
        label="退款／退货状态"
        align="center"
        prop="status"
        :formatter="statusFormat"
      />
      <!-- <el-table-column label="是否预存款支付" align="center" prop="predepositPay"  :formatter="predepositPayFormat"/> -->
      <el-table-column
        label="物流公司名称"
        align="center"
        prop="logisCompanyName"
      />
      <el-table-column label="物流单号" align="center" prop="waybillCode" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <router-link
            v-if="showRefundDetail(scope.row)"
            :to="{
              path: '/marketingManager/marketingGoods/refundreview',
              query: { id: scope.row.id }
            }"
          >
            <el-button type="text" icon="el-icon-delete" size="mini"
              >退款详情</el-button
            >
          </router-link>
          <router-link
            v-if="showReturnDetail(scope.row)"
            :to="{
              path: '/marketingManager/marketingGoods/returnreview',
              query: { id: scope.row.id }
            }"
          >
            <el-button type="text" icon="el-icon-delete" size="mini"
              >退货详情</el-button
            >
          </router-link>
          <router-link
            v-if="showRefundAudit(scope.row)"
            :to="{
              path: '/marketingManager/marketingGoods/refundreview',
              query: { id: scope.row.id }
            }"
          >
            <el-button type="text" icon="el-icon-download" size="mini"
              >退款审核</el-button
            >
          </router-link>
          <router-link
            v-if="showReturnAudit(scope.row)"
            :to="{
              path: '/marketingManager/marketingGoods/returnreview',
              query: { id: scope.row.id }
            }"
          >
            <el-button type="text" icon="el-icon-download" size="mini"
              >退货审核</el-button
            >
          </router-link>

          <el-button
            v-if="
              (scope.row.status == '2' || scope.row.status == '8') &&
                scope.row.predepositPay == '0'
            "
            type="text"
            icon="el-icon-download"
            size="mini"
            @click="paymentDialog(scope.row.id)"
            >发放
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listOmsBackOrder,
  releaseMoney,
  getOmsBackOrder,
  delOmsBackOrder,
  addOmsBackOrder,
  updateOmsBackOrder,
  exportOmsBackOrder
} from "@/api/order/OmsBackOrder";

export default {
  name: "OmsBackOrder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 退单退款表格数据
      OmsBackOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 1 退款 2 退货字典
      typeOptions: [],
      YesNOOptions: [],
      // 退款／退货状态1:退款申请 字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        backCode: undefined,
        orderCode: undefined,
        type: undefined,
        status: undefined,
        logisCompanyName: undefined,
        waybillCode: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        backCode: [
          { required: true, message: "退单号不能为空", trigger: "blur" }
        ],
        orderId: [
          {
            required: true,
            message: "订单id 对应订单表 oms_order中的id不能为空",
            trigger: "blur"
          }
        ],
        orderCode: [
          {
            required: true,
            message: "订单号 对应oms_order 表中的order_code不能为空",
            trigger: "blur"
          }
        ],
        storeId: [
          {
            required: true,
            message: "店铺id 平台的为0 不能为空",
            trigger: "blur"
          }
        ],
        customerId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "1 退款 2 退货不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("after_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("afterSaleStatus").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("ye_no01").then(response => {
      this.YesNOOptions = response.data;
    });
  },
  methods: {
    /** 查询退单退款列表 */
    getList() {
      this.loading = true;
      listOmsBackOrder(this.queryParams).then(response => {
        this.OmsBackOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //发放金额
    paymentDialog(id) {
      this.$confirm("确定发放金额吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        if (this.isProcess) {
          return;
        } else {
          this.isProcess = true;
        }
        releaseMoney(id).then(response => {
          this.isProcess = false;
          if (response.code == 10000) {
            this.getList();
            this.$message({
              type: "success",
              message: "发放成功"
            });
          } else {
            this.$message({
              type: "error",
              message: "系统异常 发放失败"
            });
          }
        });
      });
    },
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    backPriceFormat(row, column) {
      if (row.pointPrice > 0 && row.backPrice > 0) {
        return `${row.pointPrice}+￥${row.backPrice}`;
      } else if (row.pointPrice <= 0 && row.backPrice > 0) {
        return `￥${row.backPrice}`;
      } else if (row.pointPrice > 0 && row.backPrice <= 0) {
        return `${row.pointPrice}`;
      } else {
        return "";
      }
    },
    realBackPriceFormat(row, column) {
      if (row.realPointPrice > 0 && row.realBackPrice > 0) {
        return `${row.realPointPrice}+￥${row.realBackPrice}`;
      } else if (row.realPointPrice <= 0 && row.realBackPrice > 0) {
        return `￥${row.realBackPrice}`;
      } else if (row.realPointPrice > 0 && row.realBackPrice <= 0) {
        return `${row.realPointPrice}`;
      } else {
        return "";
      }
    },
    predepositPayFormat(row, column) {
      return this.selectDictLabel(this.YesNOOptions, row.predepositPay);
    },

    // 退款／退货状态1:退款申请 字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 显示退款详情
    showRefundDetail(order) {
      return (
        (order.status == "2" || order.status == "3" || order.status == "10") &&
        order.type == "1"
      );
    },
    // 显示退货详情
    showReturnDetail(order) {
      return (
        (order.status == "6" ||
          order.status == "5" ||
          order.status == "8" ||
          order.status == "9" ||
          order.status == "10") &&
        order.type == "2"
      );
    },
    // 显示退款审核
    showRefundAudit(order) {
      return order.status == "1";
    },
    // 显示退货审核
    showReturnAudit(order) {
      return order.status == "4" || order.status == "7";
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        backCode: undefined,
        orderId: undefined,
        orderCode: undefined,
        storeId: undefined,
        customerId: undefined,
        skuidNums: undefined,
        type: undefined,
        reason: undefined,
        desc: undefined,
        credential: undefined,
        backType: undefined,
        backPrice: undefined,
        realBackPrice: undefined,
        pics: undefined,
        status: undefined,
        predepositPay: undefined,
        logisCompanyName: undefined,
        waybillCode: undefined,
        createTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加退单退款";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOmsBackOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改退单退款";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateOmsBackOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addOmsBackOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除退单退款编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delOmsBackOrder(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有退单退款数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportOmsBackOrder(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>
